package com.project.ereader;

import java.util.List;

import com.project.adapters.AdapterArticle;
import com.project.adapters.AdapterPlayList;
import com.project.services.IServiceDownload;
import com.project.services.IServiceMediaPlayer;
import com.project.services.ServiceDownload;
import com.project.services.ServiceMediaPlayer;
import com.project.utility.DownloadTask;
import com.project.utility.MediaInfo;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class PlayListActivity extends Activity {

	private IServiceMediaPlayer serviceMediaPlayer;
	private AdapterPlayList adapterPlayList;
	protected ListView listView = null;
	protected ProgressBar pbMusic = null;
	protected TextView txtTitle = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_play_list);
		listView = (ListView) findViewById(R.id.listView);
		pbMusic = (ProgressBar)findViewById(R.id.pbMusic);
		txtTitle= (TextView)findViewById(R.id.txtTitle);
		initializeServices();
	}
	
	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			MediaInfo media = (MediaInfo)msg.obj;
			pbMusic.setMax(media.getDuration());
			pbMusic.setProgress(media.getCurrentPosition());
			txtTitle.setText(media.getTitle());
		};
	};
	
private void loadProgress() {
		Thread thread = new Thread(new Runnable() {
			@Override
			public void run() {
				while (true) {
					MediaInfo current = serviceMediaPlayer.getPlayingMedia();
					if(current==null){
						try {
							Thread.sleep(2000);
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						continue;
					}
					handler.sendMessage(handler.obtainMessage(0, current));
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}

			}
		});

		thread.start();

	}



	private ServiceConnection mediaPlayerServiceConnection = new ServiceConnection() {

		@Override
		public void onServiceDisconnected(ComponentName name) {

		}

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			serviceMediaPlayer = (IServiceMediaPlayer) service;
			showPlayList();
			loadProgress();
		}
	};
	
	private void showPlayList(){
		List<MediaInfo> data = serviceMediaPlayer.getPlayList();
		adapterPlayList = new AdapterPlayList(this, R.layout.list_item_play_list, data);
		adapterPlayList.setServiceMediaPlayer(serviceMediaPlayer);
		listView.setAdapter(adapterPlayList);
	}


	private void initializeServices() {
		if (serviceMediaPlayer != null){
			showPlayList();
			return;
		}
		Intent serviceMediaPlayerIntent = new Intent(this, ServiceMediaPlayer.class);
		bindService(serviceMediaPlayerIntent, mediaPlayerServiceConnection,
				Context.BIND_AUTO_CREATE);
	}

}
